﻿@using System.ComponentModel.DataAnnotations
@using System.Text.Json
@layout StudentLayout
@inject StudentClient Http
@inject NavigationManager Navigation

@if (hasError){
    <Alert Type="@AlertType.Error"
       Message="错误"
       Description="@errorMessage"
       ShowIcon="true" 
       Closable />
}
@if (loading)
{
    <p>Loading...</p>
}
else 
{
    <Breadcrumb Style="margin:2vh;" Separator=">">
		<BreadcrumbItem Href="/login/student">学生登录</BreadcrumbItem>
		<BreadcrumbItem Href="/StudentProfile">检查信息</BreadcrumbItem>
		<BreadcrumbItem>修改并提交信息</BreadcrumbItem>
    </Breadcrumb>
    <Form Model="@model"
          Loading="@isFormLoading"
          OnFinish="OnFinish"
          Size="@AntSizeLDSType.Large"
          LabelColSpan="3"
          WrapperColSpan="16">
        <FormItem WrapperColOffset="0" WrapperColSpan="16" Style="margin:3vh;">
            <h2>住宿信息核对</h2>
            <p>校外住宿同学需填写详细地址</p>
        </FormItem>
        <FormItem Label="校外住宿地址">
            <Select DataSource="@_provinceList"
                @bind-Value="@context.Provice"
                ValueName="@nameof(CNRegion.Name)"
                LabelName="@nameof(CNRegion.Name)"
                Placeholder="选择省份"
                OnSelectedItemChanged="async (region) => {_cityList.Clear(); _cityList.AddRange(await this.Http.GetRegionChildren(region.Id)); 
                            model.Village = model.Street = null;
                            _villageList.Clear(); _streetList.Clear(); }">
            </Select>
            <Select DataSource="@_cityList"
                @bind-Value="@context.City"
                ValueName="@nameof(CNRegion.Name)"
                LabelName="@nameof(CNRegion.Name)"
                Placeholder="选择城市"
                OnSelectedItemChanged="async (region) => {_villageList.Clear(); _villageList.AddRange(await this.Http.GetRegionChildren(region.Id)); 
                            model.Street = null;
                            _streetList.Clear(); }">
            </Select>
            <Select DataSource="@_villageList"
                @bind-Value="@context.Village"
                ValueName="@nameof(CNRegion.Name)"
                LabelName="@nameof(CNRegion.Name)"
                Placeholder="选择区"
                OnSelectedItemChanged="async (region) =>{_streetList.Clear(); _streetList.AddRange(await this.Http.GetRegionChildren(region.Id));}">
            </Select>
            <Select DataSource="@_streetList"
                @bind-Value="@context.Street"
                ValueName="@nameof(CNRegion.Name)"
                LabelName="@nameof(CNRegion.Name)"
                Placeholder="选择街道">
            </Select>
            <Input @bind-Value="@context.Address"
                   Placeholder="详细地址"/>
        </FormItem>
        <div style="width:100%;display:flex;flex-direction:column;align-items:center;">
            <Button Size="@ButtonSize.Large" Type="@ButtonType.Primary" HtmlType="submit">
                核对无误，确认提交
            </Button>
        </div>
        
    </Form>
}

@code {
    private bool loading = true;

    private bool isFormLoading = false;
    private bool hasError = false;
    private string errorMessage = "";

    private Model model = new();

    private List<CNRegion> _provinceList = new();
    private List<CNRegion> _cityList = new();
    private List<CNRegion> _villageList = new();
    private List<CNRegion> _streetList = new();

    protected class Model
    {
        [Required(ErrorMessage = "必填字段")]
        [MaxLength(255)]
        public string Address { get; set; } = string.Empty;
        [Required(ErrorMessage = "必填字段")]
        public string? Provice { get; set; }
        [Required(ErrorMessage = "必填字段")]
        public string? City { get; set; }
        [Required(ErrorMessage = "必填字段")]
        public string? Village { get; set; }
        [Required(ErrorMessage = "必填字段")]
        public string? Street { get; set; }
    }

    private async Task OnFinish(EditContext editContext)
    {
        isFormLoading = true;
        var success = await Http.UpdateHotel(new UpdateHotelRequest($"{model.Provice}{model.City}{model.Village}{model.Street}{model.Address}"));
        if (!success) 
        {
            SetError("提交失败，请稍后重试");
            return;
        }
        Navigation.NavigateTo("/StudentProfile");
    }

    protected override async Task OnInitializedAsync()
    {
        _provinceList = (await Http.GetProvinces()).ToList();
        loading = false;
    }

    private void SetError(string msg)
    {
        hasError = true;
        errorMessage = msg;
    }
}